/**
 * 用户管理初始化
 */
var VpnUserInfo = {
    id: "VpnUserInfoTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
VpnUserInfo.initColumn = function () {
    return [[
        {title: '用户名', field: 'userName', visible: true, align: 'center',width: 200},
        {title: '注册IP', field: 'regIP', visible: true, align: 'center', width: 150},
        {title: '注册平台', field: 'regPlatform', visible: true,
            align: 'center',width: 100,templet:function (item) {
                // 1-android,2-ios,3-window,4-mac,5-web
                var value = item.regPlatform;

                if(value == '1'){
                    return '安卓'
                }
                else if(value == '2'){
                    return 'IOS'
                }
                else if(value == '3'){
                    return 'Windows'
                }
                else if(value == '4'){
                    return  'Mac'
                }
                else if(value == '5'){
                    return 'Web'
                }
                else{
                    return ""
                }
            }},
        {title: '注册渠道', field: 'channelNo', visible: true, align: 'center', width: 100},
        {title: '注册版本', field: 'regVersion', visible: true, align: 'center', width: 100},
        {title: '登录IP', field: 'loginIP', visible: true, align: 'center', width: 150},
        {title: '登录平台', field: 'loginPlatform', visible: true,
            align: 'center',width: 100,templet:function (item) {
                // 1-android,2-ios,3-window,4-mac,5-web
                var value = item.loginPlatform;

                if(value == '1'){
                    return '安卓'
                }
                else if(value == '2'){
                    return 'IOS'
                }
                else if(value == '3'){
                    return 'Windows'
                }
                else if(value == '4'){
                    return  'Mac'
                }
                else if(value == '5'){
                    return 'Web'
                }
                else{
                    return ""
                }
            }},
        {title: '登录版本', field: 'loginVersion', visible: true, align: 'center', width: 100},
        {title: '注册时间', field: 'regTime', visible: true,
            align: 'center',width: 200},
        {title: '最后上线时间', field: 'lastOnlineTime', visible: true,
            align: 'center',width: 200},
        {title: '上次支付渠道', field: 'paymentWay', visible: true,
            align: 'center', templet: function (item) {
                var value = item.paymentWay;
                if(value == '1'){
                    return "支付宝";
                }
                else if(value == "2"){
                    return "微信支付"
                }
                else if(value == "3"){
                    return "银联"
                }
                else if(value == "4"){
                    return "Visa"
                }
                else if(value == "5"){
                    return "Master"
                }
                else if(value == "6"){
                    return "IOS内购"
                }
                else if(value == "7"){
                    return "GooglePay"
                }
                else if(value == "8"){
                    return "手动添加"
                }
                else{
                    return ""
                }
            }},
        {title: '总支付次数', field: 'paymentCount', visible: true,
            align: 'center',templet: function (item) {
                var value = item.paymentCount;
                if(value == ''){
                    return '0'
                }
                else {
                    return value;
                }
            }},
        {title: '在用套餐', field: 'packageName', visible: true, align: 'center',width: 200},
        {title: '生效时间', field: 'beginTime', visible: true, align: 'center',width: 120},
        {title: '失效时间', field: 'endTime', visible: true, align: 'center',width: 120}
    ]];
};

/**
 * 检查是否选中
 */
VpnUserInfo.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        VpnUserInfo.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加用户管理
 */
VpnUserInfo.openAddVpnUserInfo = function () {
    var index = layer.open({
        type: 2,
        title: '添加用户管理',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/vpnUserInfo/vpnUserInfo_add'
    });
    this.layerIndex = index;
};

/**
 * 重置用户密码
 */
VpnUserInfo.resetPwd = function () {
    var index = layer.open({
        type: 2,
        title: '重置用户密码',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/vpnUserInfo/vpnUserInfo_add'
    });
    this.layerIndex = index;
};
/**
 * 用户账号禁用
 */
VpnUserInfo.disableUser = function(){
    var index = layer.open({
        type: 2,
        title: '用户账号禁用',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/vpnUserInfo/vpnUserInfo_disable'
    });
    this.layerIndex = index;
}

/**
 * 手动添加VIP套餐
 */
VpnUserInfo.addVipPackage = function(){
    var index = layer.open({
        type: 2,
        title: '添加VIP套餐',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/vpnUserInfo/add_vip_package'
    });
    this.layerIndex = index;
};

/**
 * 打开查看用户管理详情
 */
VpnUserInfo.openVpnUserInfoDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '用户管理详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/vpnUserInfo/vpnUserInfo_update/' + VpnUserInfo.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除用户管理
 */
VpnUserInfo.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/vpnUserInfo/delete", function (data) {
            Feng.success("删除成功!");
            VpnUserInfo.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("vpnUserInfoId", this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询用户管理列表
 */
VpnUserInfo.search = function () {
    var queryData = {};
    queryData['userName'] = $("#userName").val();
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['packageId'] = $("#packageName").val();
    queryData['buyCondition'] = $("#buyCondition").val();

    VpnUserInfo.table.reload("tableReload",{
        page: {
            curr: 1 //重新从第1页开始
        },
        where: queryData
    });

};

VpnUserInfo.initSelectData = function () {
    var ajax = new $ax(Feng.ctxPath + "/vpnUserInfo/getFilterData", function (data) {
        var packageList = data.packageList;
        packageList.forEach(function (item) {
            $("#packageName").append("'<option value='"+item.pid+"'>"+item.pname+"</option>'");
        }) ;

    }, function (data) {
        Feng.error("筛选条件获取失败" + data.responseJSON.message + "!");
    });
    ajax.start();
};


VpnUserInfo.exportExcl = function () {
    var userName = $("#userName").val();
    var beginTime = $("#beginTime").val();
    var endTime = $("#endTime").val();
    var packageName = $("#packageName").val();
    var hrefUrl =
        Feng.ctxPath + "/vpnUserInfo/download/excel" +
        "?userName="+userName+"&beginTime="+beginTime+"&endTime="+endTime+"&packageName="+packageName;
    window.location.href = hrefUrl;
};


VpnUserInfo.tableList = function(queryData){
    layui.use("table",function () {
        var table = layui.table;

        VpnUserInfo.table = table;

        table.render({
            elem: "#table",
            url: Feng.ctxPath +"/vpnUserInfo/listPage",
            cellMinWidth: 120,
            where: queryData,
            page: true,
            limit: 20,
            cols: VpnUserInfo.initColumn(),
            parseData: function(res){
                return{
                    "code":0,
                    "msg":"获取成功",
                    "count":res.total,
                    "data": res.items
                }
            },
            text: {
                none: '暂无相关数据'
            },
            id: "tableReload"
        });
    });
};

$(function () {
    VpnUserInfo.tableList(null);

    VpnUserInfo.initSelectData();

});
